<?php
/**
* Django's templating engine (partial) PHP implementation.
* provides some template tags support
* @link http://djangoproject.com
* this file is from a webappkit package (kit)
* @link http://webappkit.net
* @package djtpl
* @author J.Ducastel <jeremie@ducastel.name>
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*/

/**
* switch node : addition to original implementation
* <code>{% switch somevar %}
* {% case anothervar %} somevar = anothervar {% endcase %}
* {% case "somevalue" %} somevar = "somevalue" {% endcase %}
* {% default %} this is the default block {% enddefault %}
* {% endswitch %}</code>
*/
class djtplTagNode_switch extends djtplTagNode {

	/**
	* builds itself from tokens
	* @param djangoTplToken &$selfToken
	* @param array &$nextTokens djangoTplToken
	* @access public
	* @return bool
	*/
	function buildFromTokens(&$selfToken,&$nextTokens) {
		$this->tagname=$selfToken->getTagName();
		$this->tagdef=$selfToken->getTagDef();
		$this->buildChildNodesFromTokens($nextTokens);
		//echo wakaffvar($this);
		return true;
	}/**/

	/**
	* is the node atomic (has no children)
	* @access public
	* @return bool
	*/
	function isAtomic() {
		return false;
	}/**/
	
	/**
	* renders node as text
	* @param djangoTplContext &$context
	* @access public
	* @return string
	*/
	function render(& $context) {
		// evaluating switcher expression
		$switcherExpression=$this->tagdef;
		// searching for case
		foreach ($this->childnodes as $node) {
			if ($node->isTag() 
			and $node->getTagName()=='case'
			and $node->compareExpression($context,$switcherExpression))
				return $node->render($context);
		}
		// searching for default
		foreach ($this->childnodes as $node) {
			if ($node->isTag() and $node->getTagName()=='default')
				return $node->render($context);
		}
		return '';
	}/**/
}/* end of class djtplTagNode_switch */

/**
* @see djangoTplTagNode_switch
*/
class djtplTagNode_case extends djtplTagNode {

	/**
	* is the node atomic (has no children)
	* @access public
	* @return bool
	*/
	function isAtomic() {
		return false;
	}/**/
	
	/**
	* renders
	* @param djangoTplContext
	* @access public
	* @return string
	*/
	function render(&$context) {
		return $this->renderChildNodes($context);
	}/**/
	
	/**
	* compares expression's value with self value
	* @access public
	* @param djangotplcontext &$context
	* @param string expression
	* @return bool
	*/
	function compareExpression(&$context,$expression) {
		$value=djtplParser::evalExpression($context,$expression);
		$selfValue=djtplParser::evalExpression($context,$this->tagdef);
		return $value==$selfValue;
	}/**/
}/* end of djangoTplTagNode_case */

/**
* @see djangoTplTagNode_switch
*/
class djtplTagNode_default extends djtplTagNode {
	/**
	* is the node atomic (has no children)
	* @access public
	* @return bool
	*/
	function isAtomic() {
		return false;
	}/**/
}/* end of djtplTagNode_case class */
?>
